package com.example.upgradesystem.controller;

import com.example.upgradesystem.model.UpgradeTask;
import com.example.upgradesystem.repository.PackageRepository;
import com.example.upgradesystem.repository.UpgradeTaskRepository;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

@RestController
@RequestMapping("/api/tasks")
public class UpgradeTaskController {
    @Resource
    private UpgradeTaskRepository upgradeTaskRepository;

    @Resource
    private PackageRepository packageRepository;

    @PostMapping
    public ResponseEntity<?> submitTask(@RequestParam("packageId") Long packageId) {
        // 创建UpgradeTask对象
        UpgradeTask task = new UpgradeTask();
        task.setUpgradePackage(packageRepository.findById(packageId).get());
        task.setStatus("Pending");
        task.setCreateDate(new Date());

        // 保存到数据库
        upgradeTaskRepository.save(task);

        return ResponseEntity.ok("Task submitted successfully.");
    }

    @GetMapping("/{id}/status")
    public ResponseEntity<?> getTaskStatus(@PathVariable Long id) {
        UpgradeTask task = upgradeTaskRepository.findById(id).get();
        return ResponseEntity.ok(task.getStatus());
    }
}